package com.share.im;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.share.im.exception.AppException;
import com.share.im.interactor.ILoginInteractor;
import com.share.im.interactor.LoginInteractor;
import com.share.im.util.db.DhDB;
import com.share.im.util.ioc.Instance.InstanceScope;
import com.share.im.util.ioc.IocContainer;
import com.share.im.util.log.Const;

public class IMApplication extends Application
{

	public static final int NETTYPE_WIFI = 0x01;
	public static final int NETTYPE_CMWAP = 0x02;
	public static final int NETTYPE_CMNET = 0x03;

	private static IMApplication mInstance = null;

	@Override
	public void onCreate()
	{
		super.onCreate();
		mInstance = this;
		// StrictMode.setThreadPolicy(new
		// StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
		// StrictMode.setVmPolicy(new
		// StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

		// 全局异常
		Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());

		// IOC的初始化
		IocContainer.getShare().initApplication(this);

		// IocContainer.getShare().bind(UserModel.class).to(IUserModel.class);
		IocContainer.getShare().bind(LoginInteractor.class).to(ILoginInteractor.class).scope(InstanceScope.SCOPE_SINGLETON);;

		// 数据库初始化
		DhDB db = IocContainer.getShare().get(DhDB.class);
		db.init("smim", Const.DATABASE_VERSION);
	}

	public static IMApplication getInstance()
	{
		return mInstance;
	}

	/**
	 * 检测网络是否可用
	 * 
	 * @return
	 */
	public boolean isNetworkConnected()
	{
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		return ni != null && ni.isConnectedOrConnecting();
	}

	/**
	 * 获取App安装包信息
	 * 
	 * @return
	 */
	public PackageInfo getPackageInfo()
	{
		PackageInfo info = null;
		try
		{
			info = getPackageManager().getPackageInfo(getPackageName(), 0);
		}
		catch (NameNotFoundException e)
		{
			e.printStackTrace(System.err);
		}
		if (info == null)
			info = new PackageInfo();
		return info;
	}
	
	public String getResourceMessage(int id){
		return getResources().getString(id);
	}
}